var dataTableConfig = {
		"processing" : true,
		"serverSide" : true,
		"paging" : true,
		"lengthChange" : false,
		"searching" : false,
		"ordering" : false,
		"info" : true,
		"autoWidth" : false,
		"oLanguage" : {
			"sProcessing" : "正在加载中......",
			"sLengthMenu" : "每页显示 _MENU_ 条记录",
			"sZeroRecords" : "对不起，查询不到相关数据！",
			"sEmptyTable" : "表中无数据存在！",
			"sInfo" : "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",
			"sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
			"sSearch" : "搜索",
			"oPaginate" : {
				"sFirst" : "首页",
				"sPrevious" : "上一页",
				"sNext" : "下一页",
				"sLast" : "末页"
			}
		},
		"oTableTools" : {
			"sRowSelect" : "multi",
			"aButtons" : [
					{
						"sExtends" : "new_record",
						"sButtonText" : "Add"
					},
					{
						"sExtends" : "select",
						"sButtonText" : "Delete Recods",
						"fnClick" : function(nButton,
								oConfig, oFlash) {
							//delete stuff comes here 
							alert('test');
						}

					} ]
		},
		ajax : "/component/queryComponent",
		columns : [
				{
					data : "id",
					render : function(data, type, full) {
						return '<input name=\"items\" type="checkbox" id=\"check_'+data+'\" value="' + data + '"/>';
					}
				}, {
					"data" : "id"
				},{
					"data" : "bean_class"
				}, {
					"data" : "bean_class_path"
				}, {
					"data" : "state_name"
				}, {
					"data" : "create_date"
				}, {
					"data" : "comment"
				}, {
					"data" : "url_path"
				}, {
					"data" : "state"
				}],
		columnDefs: [{    
		             	"targets": [8], 
						"visible": false    
		             }], 
		"dom" : "<'row'<'col-xs-2'l><'#mytool.col-xs-4'><'col-xs-6'f>r>"
				+ "t"
				+ "<'row'<'col-xs-6'i><'col-xs-6'p>>",
};

//初始化表格
var table = $('#component_table').DataTable(dataTableConfig);

//全选
$(function() {
	//全选
	$("#checkall_component").click(function() {
		$('[name=items]:checkbox').prop("checked", this.checked);
	});
	$('[name=items]:checkbox').click(
		function() {
			var $tmp = $('[name=items]:checkbox');
			$('#checkall_component').prop('checked',
					$tmp.length == $tmp.filter(':checked').length);
		}	
	);
});

var app = angular.module('componentApp', []);

var initController = app.controller("componentController" , function($scope, $http){
	$('#addModal_component').on('hide.bs.modal', function () {
		document.getElementById("addForm_component").reset(); 
	});
	$scope.add = function(){
		var form = document.getElementById("addForm_component");
		var bean_class = form.bean_class_component.value;
		var bean_class_path = form.bean_class_path_component.value;
		var state = form.state_component.value;
		var comment = form.comment_component.value;
		var url_path = form.url_path_component.value;
		$http({
			url : '/component/add',
			params:{bean_class:bean_class, bean_class_path:bean_class_path, state:state, comment:comment , 
				url_path:url_path},
			method : 'GET'
		}).success(function(data, header, config, status) {
			// 响应成功
			if(data=="1"){
				$('#addSuccessAlert_component').fadeIn(2000);
				$('#addSuccessAlert_component').fadeOut(2000);
				table.draw();
			}else{
				$('#addFailedAlert_component').fadeIn(2000);
				$('#addFailedAlert_component').fadeOut(2000);
			}
		}).error(function(data, header, config, status) {
			// 处理响应失败
			$('#addFailedAlert_component').fadeIn(2000);
			$('#addFailedAlert_component').fadeOut(2000);
		});
	};
	$scope.del_init=function(){
		var ids = "";
		$('[name=items]:checkbox:checked').each(function() {
			ids += $(this).val() + ";";
		});
		if(ids==""){
			$('#selAlert_component').html("<strong>请选择记录进行删除！</strong>");
			$('#alertModal_component').modal("toggle");
		}else{
			$("#delModal_component").modal("toggle");
		}
		
	};
	$scope.del = function(){
		var ids = "";
		$('[name=items]:checkbox:checked').each(function() {
			ids += $(this).val() + ";";
		});
		if(ids==""){
			alert("请选择要删除的记录！");
		}else{
			$http({
				url : '/component/delete',
				params:{ids:ids},
				method : 'GET'
			}).success(function(data, header, config, status) {
				// 响应成功
				if(data==true){
					$('#promptContent_component').hide();
					$('#del_component').hide();
					$('#deleteSuccessAlert_component').fadeIn(2000);
					$('#delModal_component').on('hide.bs.modal', function () {
						table.draw();
						$('#del_component').show();
						$('#deleteSuccessAlert_component').hide();
						$('#promptContent_component').show();
					});
				}else{
					$('#promptContent_component').hide();
					$('#del_component').hide();
					$('#deleteFailedAlert_component').fadeIn(2000);
					$('#delModal_component').on('hide.bs.modal', function () {
						table.draw();
						$('#del_component').show();
						$('#deleteFailedAlert_component').hide();
						$('#promptContent_component').show();
					});
				}
				
			}).error(function(data, header, config, status) {
				// 处理响应失败
				$('#promptContent_component').hide();
				$('#del_component').hide();
				$('#deleteFailedAlert_component').fadeIn(2000);
				$('#delModal_component').on('hide.bs.modal', function () {
					$('#del_component').show();
					$('#deleteFailedAlert_component').hide();
					$('#promptContent_component').show();
				});
			});
		}
	};
	$scope.update_init =function(){
		var ids = "";
		$('[name=items]:checkbox:checked').each(function() {
			ids += $(this).val() + ";";
		});
		if(ids==""){
			$('#selAlert_component').html("<strong>请选择要修改的记录！</strong>");
			$('#alertModal_component').modal("toggle");
		}else if(ids.split(";").length>2){
			$('#selAlert_component').html("<strong>修改的记录数不能大于1.</strong>");
			$('#alertModal_component').modal("toggle");
		}else{
			  
			 $("table :checkbox").each(function(key,value){
		            if($(value).prop('checked')){
		            	var selRow =$('#component_table').dataTable().fnGetData(key-1);
		            	$scope.id_update = selRow.id;
		            	$scope.bean_class_update = selRow.bean_class;
		            	$scope.bean_class_path_update = selRow.bean_class_path;
		            	$scope.state_update = selRow.state;
		            	$scope.state_name_update = selRow.state_name;
		            	$scope.comment_update = selRow.comment;
		            	$scope.url_path_update = selRow.url_path;
		            }
			 });
			 $("#updateModal_component").modal("toggle");
		}
	};
	$scope.update = function(){
		var form = document.getElementById("updateForm_component");
		var id = form.id_update_component.value;
		var bean_class = form.bean_class_update_component.value;
		var bean_class_path = form.bean_class_path_update_component.value;
		var state = form.state_update_component.value;
		var comment = form.comment_update_component.value;
		var url_path = form.url_path_update_component.value;
		$http({
			url : '/component/edit',
			params:{id:id, bean_class:bean_class, bean_class_path:bean_class_path, state:state , comment:comment, 
				url_path:url_path},
			method : 'GET'
		}).success(function(data, header, config, status) {
			// 响应成功
			if(data=="1"){
				$('#updateSuccessAlert_component').fadeIn(2000);
				$('#updateSuccessAlert_component').fadeOut(2000);
				table.draw();
			}else{
				$('#updateFailedAlert_component').fadeIn(2000);
				$('#updateFailedAlert_component').fadeOut(2000);
			}
		}).error(function(data, header, config, status){
			// 处理响应失败
			$('#updateFailedAlert_component').fadeIn(2000);
			$('#updateFailedAlert_component').fadeOut(2000);
		});
	}
	
	$scope.query = function () {
		var url = '/component/queryComponent';
		var data = {};
		var form =document.getElementById("queryForm_component");  // $('#queryForm');
		if(form.bean_class_query_component.value!=0){
			data.bean_class =  form.bean_class_query_component.value;
		}
		$('#queryBtn_component').attr("disabled", "true");
		
		$('#component_table').dataTable().api().destroy();
		dataTableConfig.ajax={"url": url, "data":data};
		dataTableConfig.fnDrawCallback = function(settings){
			$('#queryBtn_component').removeAttr("disabled");
		};
		table=$('#component_table').dataTable(dataTableConfig);
	};
});